package com.voxel.sense.infomsg.module.firm.details.general;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.com.common.app.PresenterFragment;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.view.EmptyView;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.bean.FirmTableInfoBean;
import com.voxel.sense.infomsg.view.decoration.ItemDivider;
import com.voxel.sense.infomsg.view.decoration.LinearItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cyy.example.com.facotry.constants.IntentConstants;

/**
 * Created by chenyiyao on 2017/11/8.
 * 公司基本信息
 */

public class FirmGeneralInfoFrag extends PresenterFragment<FirmGeneralContract.Presenter>
        implements FirmGeneralContract.View<List<FirmTableInfoBean>> {
    @BindView(R.id.recycle_view)
    RecyclerView recycle_view;
    @BindView(R.id.empty_view)
    EmptyView empty_view;

    private List<FirmTableInfoBean> mListData;
    private FirmGeneralExpandAdapter mAdapter;
    private String mCompanyid;

    @Override
    protected FirmGeneralContract.Presenter initPresenter() {
        return new FirmGeneralPresenter(this);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.frag_firm_detail_recycler;
    }

    @Override
    protected void initArgs(Bundle bundle) {
        super.initArgs(bundle);
        if (bundle == null)
            return;
        mCompanyid = bundle.getString(IntentConstants.STRING_TYPE);

    }

    @Override
    protected void initData() {
        super.initData();
        recycle_view.setLayoutManager(new LinearLayoutManager(getActivity()));
        //设置下划线
        recycle_view.addItemDecoration(new LinearItemDecoration(R.drawable.recycle_divider));
        mListData = new ArrayList<>();
        mAdapter = new FirmGeneralExpandAdapter(new ArrayList<FirmTableInfoBean>());
        recycle_view.setAdapter(mAdapter);
        empty_view.bind(recycle_view);
        empty_view.setRefreshBtnVisible(false);
        mAdapter.expandAll();
        mPresenter.doLoadData(mCompanyid);
    }

    @Override
    public void onRefreshData(List<FirmTableInfoBean> data) {
        mAdapter.replace(data);
        mAdapter.expandAll();
        empty_view.triggerOkOrEmpty(mAdapter.getData().size() > 0 ? true : false);
    }

    @Override
    public void onShowDownPogress() {
        showProgressDialog("");
    }

    @Override
    public void onDismissDownProgress() {
        dismissProgressDialog();
    }

    @Override
    public void showLoading(String str) {
        empty_view.triggerLoading();
    }

    @Override
    public void dimissLoading() {
        boolean b = mAdapter.getItemCount() > 0;
        if (b) {
            empty_view.triggerOk();
        } else if (!NetworkUtils.isConnected()) {
            empty_view.triggerNetError();
        } else {
            empty_view.triggerEmpty();
        }
    }

    @Override
    public void showError(String str) {
        super.showError(str);
        empty_view.triggerError(str);
    }
}
